﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_Admin_CapNhatNguoiDung : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            layMaNguoiDung();

            layTenNguoiDung();

            layDanhSachLoaiNguoiDung();

            layDanhSachPhong();

            layDanhSachChucVu();

            layMatKhau();

            layDienThoaiDiDong();

            layDienThoaiNhaRieng();

            layEmail();

            layDanhSachBiXoa();
        }
    }

    protected void layMaNguoiDung()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        NguoiDungBUS ndBUS = new NguoiDungBUS();

        string maNguoiDung = ndBUS.layMaNguoiDung(maNguoiDungURL);

        txtMaNguoiDung.Text = maNguoiDung;
    }

    protected void layTenNguoiDung()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        NguoiDungBUS ndBUS = new NguoiDungBUS();

        string tenNguoiDung = ndBUS.layTenNguoiDung(maNguoiDungURL);

        txtTenNguoiDung.Text = tenNguoiDung;
    }

    protected void layDanhSachLoaiNguoiDung()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();

        lndBUS.themItemVaoDropDownList(drplstMaLoaiNguoiDung);

        string maLoaiNguoiDung = lndBUS.layMaLoaiNguoiDungTheoMaNguoiDung(maNguoiDungURL);
        lndBUS.sapxepLoaiNguoiDung(drplstMaLoaiNguoiDung,maLoaiNguoiDung);
    }

    protected void layDanhSachPhong()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        PhongBUS pBUS = new PhongBUS();

        pBUS.themItemVaoDropDownList(drplstMaPhong);

        string maPhong = pBUS.layMaPhongTheoMaNguoiDung(maNguoiDungURL);
        pBUS.sapxepPhong(drplstMaPhong, maPhong);
    }

    protected void layDanhSachChucVu()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        ChucVuBUS cvBUS = new ChucVuBUS();

        cvBUS.themItemVaoDropDownList(drplstMaChucVu);

        string maChucVu = cvBUS.layMaChucVuTheoMaNguoiDung(maNguoiDungURL);
        cvBUS.sapxepChucVu(drplstMaChucVu, maChucVu);
    }

    protected void layMatKhau()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        NguoiDungBUS ndBUS = new NguoiDungBUS();

        string matKhau = ndBUS.layMatKhau(maNguoiDungURL);

        /*txtMatKhau.Text = matKhau;
        txtNhapLaiMatKhau.Text = matKhau;*/

        //Dung HiddenField
        hfMatKhau.Value = matKhau;
        hfNhapLaiMatKhau.Value = matKhau;
    }

    protected void layDienThoaiDiDong()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        NguoiDungBUS ndBUS = new NguoiDungBUS();

        string dtdd = ndBUS.layDienThoaiDiDong(maNguoiDungURL);

        txtDienThoaiDiDong.Text = dtdd;
    }

    protected void layDienThoaiNhaRieng()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        NguoiDungBUS ndBUS = new NguoiDungBUS();

        string dtnr = ndBUS.layDienThoaiNhaRieng(maNguoiDungURL);

        txtDienThoaiNhaRieng.Text = dtnr;
    }

    protected void layEmail()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        NguoiDungBUS ndBUS = new NguoiDungBUS();

        string email = ndBUS.layEmail(maNguoiDungURL);

        txtEmail.Text = email;
    }

    protected void layDanhSachBiXoa()
    {
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];

        NguoiDungBUS ndBUS = new NguoiDungBUS();

        //ndBUS.themItemVaoDropDownList(drplstBiXoa);
        
        ndBUS.sapxepBiXoa(drplstBiXoa, maNguoiDungURL);
    }

    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        MyTextBox myTextBox = new MyTextBox();

        MyDropDownList myDropDownList = new MyDropDownList();

        //Kiem tra rong
        if (myTextBox.kiemtraRong(txtMaNguoiDung) == true)
        {
            Alert.Show("Ma nguoi dung khong duoc rong");
            return;
        }

        if (myTextBox.kiemtraRong(txtTenNguoiDung) == true)
        {
            Alert.Show("Ten nguoi dung khong duoc rong");
            return;
        }

        if (myDropDownList.kiemtraRong(drplstMaLoaiNguoiDung) == true)
        {
            Alert.Show("Ma loai nguoi dung khong duoc rong");
            return;
        }

        if (myDropDownList.kiemtraRong(drplstMaPhong) == true)
        {
            Alert.Show("Ma phong khong duoc rong");
            return;
        }

        if (myDropDownList.kiemtraRong(drplstMaChucVu) == true)
        {
            Alert.Show("Ma chuc vu khong duoc rong");
            return;
        }

        /*if (myTextBox.kiemtraRong(txtMatKhau) == true)
        {
            //Alert.Show("Mat khau khong duoc rong");
            txtMatKhau.Text = hfMatKhau.Value;
        }

        if (myTextBox.kiemtraRong(txtNhapLaiMatKhau) == true)
        {
            //Alert.Show("Mat khau nhap lai khong duoc rong");
            txtNhapLaiMatKhau.Text = hfNhapLaiMatKhau.Value;
        }*/

        if (myTextBox.mahoaSHA1(txtNhapLaiMatKhau) != myTextBox.mahoaSHA1(txtMatKhau))
        {
            Alert.Show("Mat khau nhap lai khong chinh xac");
            return;
        }

        if (myTextBox.kiemtraRong(txtDienThoaiDiDong) == true)
        {
            Alert.Show("Dien thoai di dong khong duoc rong");
            return;
        }

        if (myTextBox.kiemtraRong(txtDienThoaiNhaRieng) == true)
        {
            Alert.Show("Dien thoai nha rieng khong duoc rong");
            return;
        }

        if (myTextBox.kiemtraRong(txtEmail) == true)
        {
            Alert.Show("Email khong duoc rong");
            return;
        }

        if (myDropDownList.kiemtraRong(drplstBiXoa) == true)
        {
            Alert.Show("Bi xoa khong duoc rong");
            return;
        }

        //Cap nhat
        NguoiDung ndDTO = new NguoiDung();
        ndDTO.MaNguoiDung = txtMaNguoiDung.Text.ToLower();
        ndDTO.TenNguoiDung = txtTenNguoiDung.Text.ToUpper();
        ndDTO.MaLoaiNguoiDung = drplstMaLoaiNguoiDung.SelectedValue.ToString();
        ndDTO.MaPhong = drplstMaPhong.SelectedValue.ToString();
        ndDTO.MaChucVu = drplstMaChucVu.SelectedValue.ToString();
        if (myTextBox.kiemtraRong(txtMatKhau) == false && myTextBox.kiemtraRong(txtNhapLaiMatKhau) == false)
        {
            ndDTO.MatKhau = myTextBox.mahoaSHA1(txtMatKhau);
        }
        else
        {
            ndDTO.MatKhau = hfMatKhau.Value;
        }
        ndDTO.DienThoaiDiDong = txtDienThoaiDiDong.Text;
        ndDTO.DienThoaiNhaRieng = txtDienThoaiNhaRieng.Text;
        ndDTO.Email = txtEmail.Text.ToLower();
        ndDTO.BiXoa = Convert.ToByte(drplstBiXoa.SelectedValue.ToString());

        NguoiDungBUS ndBUS = new NguoiDungBUS();
        string maNguoiDungURL = Request.QueryString["MaNguoiDung"];
        ndBUS.suaNguoiDung(ndDTO, maNguoiDungURL);

        Response.Redirect("QuanLyNguoiDung.aspx");
    }

    protected void btnDong_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuanLyNguoiDung.aspx");
    }
}